package com.rc.saas.tenant.service.ticket;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.mapper.ticket.RcCouponTemplateApplyMapper;
import com.rc.saas.tenant.search.operator.CouponApplySearch;
import com.rc.saas.tenant.service.tenant.TenantRoleService;
import com.rc.saas.tenant.vo.tenant.TenantUserRoleVo;
import com.rc.saas.tenant.vo.ticket.CouponTemplateApplyVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class RcCouponTemplateApplyServiceImpl implements RcCouponTemplateApplyService {

    @Autowired
    private RcCouponTemplateApplyMapper couponTemplateApplyMapper;

    @Autowired
    private TenantRoleService tenantRoleService;

    @Override
    public RcCouponTemplateApplyMapper getMapper() {
        return couponTemplateApplyMapper;
    }

    @Override
    public PageInfo listPageCouponApply(CouponApplySearch search) {
        TenantUserRoleVo userRoleVo =  tenantRoleService.getLonginRole(search.getLoginName());
        if (userRoleVo!=null){
            search.setRoleType(userRoleVo.getRoleType());
            search.setDataAuth(userRoleVo.getDataAuth());
            switch (userRoleVo.getRoleType()){
                case "1"://自定义用户
                    search.setOperatorIds(
                            StringUtils.isNotBlank(userRoleVo.getOperatorIds())?
                                    Arrays.asList(userRoleVo.getOperatorIds().split(","))
                                    :new ArrayList<String>());
                    search.setEnterpriseIds(
                            StringUtils.isNotBlank(userRoleVo.getEnterpriseIds())?
                                    Arrays.asList(userRoleVo.getEnterpriseIds().split(","))
                                    :new ArrayList<String>());
                    break;
                case "2"://加盟商用户
                    search.setOperatorIds(StringUtils.isNotBlank(userRoleVo.getOperatorIds())?
                            Arrays.asList(userRoleVo.getOperatorIds().split(","))
                            :new ArrayList<String>());
                    break;
            }
        }
        PageHelper.startPage(search.getOffset(), search.getLimit());
        List<CouponTemplateApplyVo> list = couponTemplateApplyMapper.listCouponApply(search);
        return new PageInfo(list);
    }
}
